﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MegavaniaX.World;
using MegavaniaX.Messaging.Events;

namespace MegavaniaX.Actors.States
{
    public class PlayerStateJump : PlayerStateBase
    {
        private float _initialVelocityY;

        private static MegavaniaEventType[] _eventsAllowed = new MegavaniaEventType[]
        {
            MegavaniaEventType.JumpReleased,
            MegavaniaEventType.MoveRightPressed,
            MegavaniaEventType.MoveRightReleased,
            MegavaniaEventType.MoveLeftPressed,
            MegavaniaEventType.MoveLeftReleased,
            MegavaniaEventType.ShootPressed,
            MegavaniaEventType.ClimbPressed
        };      

        public PlayerStateJump(Player player, float initialVelocityY)
            : base(player)
        {
            NextState = new PlayerStateIdle(_player);
            _initialVelocityY = initialVelocityY;
        }


        public override void OnEnter()
        {
            _player.velocity.Y = _initialVelocityY;
            _player.acceleration.Y = 0.0010f;
            _player.CurrentAnimation = _player.animationJump;
        }

        public override bool Update(TimeSpan elapsedTime, Screen screen)
        {
            if (base.Update(elapsedTime, screen))
                return true;

            if (_player.IsActorOnSolidGround(screen))
            {
                if (_player.velocity.X == 0.0f)
                    NextState = new PlayerStateIdle(_player);
                else if (_player.velocity.X > 0)
                    NextState = new PlayerStateMove(_player, Player.InitialXVelo);
                else // _player.velocity.X < 0
                    NextState = new PlayerStateMove(_player, -Player.InitialXVelo);
                return true;
            }


            return false;

        }

        public override bool CanHandleEvent(MegavaniaEventType evtType)
        {
            return _eventsAllowed.Contains(evtType);
        }

        public override bool HandleEvent(IMegavaniaEvent evt)
        {
            if (evt.EventType == MegavaniaEventType.JumpReleased && _player.velocity.Y < 0.0f)
                _player.velocity.Y = 0.0f;


            if (evt.EventType == MegavaniaEventType.MoveRightPressed && _player.velocity.X <= 0)
            {
                _player.velocity.X = Player.InitialXVelo;
                _player.IsFacingLeft = false;
            }
            else if (evt.EventType == MegavaniaEventType.MoveLeftPressed && _player.velocity.X >= 0)
            {
                _player.velocity.X = -Player.InitialXVelo;
                _player.IsFacingLeft = true;
            }
            else if ((evt.EventType == MegavaniaEventType.MoveRightReleased && _player.velocity.X > 0) || (evt.EventType == MegavaniaEventType.MoveLeftReleased && _player.velocity.X < 0))
            {
                _player.velocity.X = 0.0f;                
            }
            else if (evt.EventType == MegavaniaEventType.ShootPressed)
            {
                _player.CurrentAnimation = _player.animationShootJumping;
            }
            else if (evt.EventType == MegavaniaEventType.ClimbPressed)
            {
                var cevt = (ClimbEvent)evt;
                if (HandleClimbEvent(cevt))
                    return true;
            }


            return false;
        }

        public override void StopShootingAnimation()
        {
            _player.CurrentAnimation = _player.animationJump;
        }

        public override void OnExit()
        {

        }
    }
}
